package cn.zxh.csdn_maven.work.flow.login.chains;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import cn.zxh.csdn_maven.base.pojo.Cookies;
import cn.zxh.csdn_maven.base.util.Random4;
import cn.zxh.csdn_maven.work.flow.login.AbstractFlowController;

public class CrossCookieFlow extends AbstractFlowController {

	@Override
	public Map<String, Object> execute(HttpClient client,
			Map<String, Object> params) throws ClientProtocolException,
			IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		String url = getBaseUrl()+"?"+Random4.getData();
		/**获取跨域信息*/
		Map<String, Object> param = getBaseParam();
		for(Entry<String, Object> entry : param.entrySet()){
			if("".equals(entry.getValue())){
				url+="&"+entry.getKey()+"=";
			}else {
				url+="&"+entry.getKey()+"="+entry.getValue();
			}
		}
		/**页面构造完成。开始访问*/
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = client.execute(httpGet);
		if(response.getStatusLine().toString().indexOf("200")!=-1){
			result.put("msg", "success");
		}
		httpGet.releaseConnection();
		/** 第一时间设置cookie */
		Cookies.setCookies(response.getHeaders("Set-Cookie"));
		return result;
	}

	@Override
	public String getBaseUrl() {
		// TODO Auto-generated method stub
		String url="https://dc.csdn.net/track";
		return url;
	}

	public Map<String, Object> getBaseParam(){
		Map<String, Object> result = new HashMap<String, Object>();
		String user_name="";
		String x_acl_token ="status_js_dkuyqthzbajmncbsb_token";
		String pid="passport";
		String oid="";
		String referrer="";
		result.put("user_name", user_name);
		result.put("x-acl-token", x_acl_token);
		result.put("pid", pid);
		result.put("oid", oid);
		result.put("referrer", referrer);
		return result;
	}
}
